package com.luo.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luo.constant.RedisConstant;
import com.luo.dao.SetMealDao;
import com.luo.dao.SetMealTravelGroupDao;
import com.luo.entity.QueryPageBean;
import com.luo.pojo.SetMeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 * @author Zoro
 * @date 2021年08月26
 */
@Service(interfaceClass = SetMealService.class)
@Transactional(readOnly = true)
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private SetMealTravelGroupDao setMealTravelGroupDao;

    @Autowired
    private JedisPool jedisPool;


    @Override
    public PageInfo<SetMeal> setMealPageSelect(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询到的
        List<SetMeal> setMeals = setMealDao.setMealPageSelect(queryPageBean.getQueryString());
        return new PageInfo<>(setMeals);
    }


    @Override
    @Transactional(readOnly = false)
    public void setMealAdd(Integer[] travelGroupIds, SetMeal setMeal) {
        //将图片名称存入到redis中去（数据库图片）
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setMeal.getImg());
        //先添加套餐有信息
        setMealDao.setMealAdd(setMeal);
        //在添加跟团游数据列表
        setMealTravelGroupAdd(travelGroupIds, setMeal.getId());
    }

    @Override
    public SetMeal getSetMealById(Integer setMealId) {
        return setMealDao.getSetMealById(setMealId);
    }

    @Override
    @Transactional(readOnly = false)
    public void setMealEdit(Integer[] travelGroupIds, SetMeal setMeal) {
        SetMeal oldSetMeal = getSetMealById(setMeal.getId());
        //修改数据
        setMealDao.setMealEdit(setMeal);
        //先删除关联数据
        setMealTravelGroupDao.deleteAllById(setMeal.getId());
        //在添加关联数据列表
        setMealTravelGroupAdd(travelGroupIds, setMeal.getId());

        //将旧图片名称在redis删除（数据库图片）
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES, oldSetMeal.getImg());
        //将图片名称存入到redis中去（数据库图片）
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setMeal.getImg());
    }

    @Override
    @Transactional(readOnly = false)
    public void setMealDeleteById(Integer setMealId) {
        SetMeal setMeal = getSetMealById(setMealId);
        //将图片名称在redis删除（数据库图片）
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setMeal.getImg());
        //先删除关联数据
        setMealTravelGroupDao.deleteAllById(setMeal.getId());
        //再删除自己本身的数据
        setMealDao.setMealDeleteById(setMeal.getId());
    }

    @Override
    public List<SetMeal> selectAll() {
        return setMealDao.selectAll();
    }

    @Override
    public SetMeal selectSetMealDetailById(Integer id) {
        return setMealDao.selectSetMealDetailById(id);
    }


    @Override
    public List<Map<String, Object>> findSetMealCount() {
        return setMealDao.findSetMealCount();
    }


    @Transactional(readOnly = false)
    public void setMealTravelGroupAdd(Integer[] travelGroupIds, Integer setMealId) {
        if (travelGroupIds.length > 0) {
            setMealTravelGroupDao.addList(travelGroupIds, setMealId);
        }
    }
}
